package com.mew.user.controller;

import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.user.SimpleUserResponseMessage;
import com.mew.user.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author dylin
 * @create 2021/4/7
 */
@Controller
@RequestMapping("/friend")
public class FriendController {
    @Autowired
    private FriendService friendService;

    /**
     * 申请列表
     * @param userId
     * @return
     */
    @GetMapping("/applys")
    @ResponseBody
    public SimpleUserResponseMessage getFriendApplys(@RequestParam("userId")long userId){
        return friendService.getFriendApplys(userId);
    }

    /**
     * 好友列表
     * @param userId
     * @return
     */
    @GetMapping("/friends")
    @ResponseBody
    public SimpleUserResponseMessage getFriends(@RequestParam("userId")long userId){
        return friendService.getFriends(userId);
    }
    /**
     * 推荐好友列表
     * @param userId
     * @return
     */
    @GetMapping("/random")
    @ResponseBody
    public SimpleUserResponseMessage getRandomUser(@RequestParam("userId")long userId,@RequestParam("limit")int limit){
        return friendService.getRandomUser(userId,limit);
    }
    /**
     * 操作申请
     * @return
     */
    @GetMapping("/apply_update")
    @ResponseBody
    public int updateApply(@RequestParam("userId")long userId,@RequestParam("friendId")long friendId,@RequestParam("status")int status){
        if(friendService.isFriend(userId,friendId)){
            friendService.deleteApply(userId,friendId);
            return ResponseCode.IsFriend.code();
        }
        try{
            if(status == 1){//同意
                friendService.agreeApply(userId, friendId);
            }else if(status == 2){//拒绝
                friendService.refuseApply(userId, friendId);
            }else{
                return ResponseCode.ParamInvalid.code();
            }
            return ResponseCode.Success.code();
        }catch (Exception e){
            return ResponseCode.SysException.code();
        }
    }
    /**
     * 添加好友
     * @return
     */
    @GetMapping("/add")
    @ResponseBody
    public int addFriend(@RequestParam("userId")long userId,@RequestParam("friendId")long friendId){
        try{
            if(friendService.getCountFriends(userId)>49){
                return ResponseCode.FriendMax.code();
            }
            if(friendService.isFriend(userId,friendId)){
                return ResponseCode.IsFriendCannotAdd.code();
            }
            friendService.addFriendApply(userId,friendId);
            return ResponseCode.Success.code();
        }catch (Exception e){
            return ResponseCode.SysException.code();
        }
    }

    /**
     * 删除好友
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public int deleteFriend(@RequestParam("userId")long userId,@RequestParam("friendId")long friendId){
        return friendService.deleteFriend(userId,friendId);
    }

    /**
     * 赠送
     * @return
     */
    @GetMapping("/give")
    @ResponseBody
    public int give(@RequestParam("userId")long userId,@RequestParam("friendId")long friendId){
        try{
            if(!friendService.isFriend(userId,friendId)){
                return ResponseCode.notFriend.code();
            }
            friendService.give(userId,friendId);
            return ResponseCode.Success.code();
        }catch (Exception e){
            return ResponseCode.SysException.code();
        }
    }
    /**
     * 全部赠送
     * @return
     */
    @GetMapping("/give_batch")
    @ResponseBody
    public int giveAll(@RequestParam("userId")long userId){
        friendService.batchGive(userId);
        return ResponseCode.Success.code();
    }
}
